package cn.com.justing.pigeon.shortener;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class IsGdLinkShortener implements LinkShortener
{
	private static Log log = LogFactory.getLog(IsGdLinkShortener.class);;

	static String apiString = "http://is.gd/api.php?longurl=";

	public IsGdLinkShortener()
	{
		super();

	}

	public String shortenLink(URL url)
	{
		return shortenLink(url.toString());
	}

	@Override
	public String shortenLink(String urlString)
	{
		String str = null;

		log.info("Trying to shorten link...");
		try
		{
			URL url = new URL(apiString + urlString);
			BufferedReader in = new BufferedReader(new InputStreamReader(url
					.openStream()));
			str = in.readLine();
			log.debug("Got link");
		} catch (Exception e)
		{
			e.printStackTrace();
			log.error(e.getStackTrace());
		}

		return str;
	}

}
